<?php
/**
 * 《我的信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */

require_once(dirname(__FILE__) . '/../emp/Info.class.php');

class My extends Info {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function layout($controller, $request) {		
		$data = array(
			'我的资料' => array('cc'=>'my.php?do=Profile.layout', 'js'=>''),		
			// '我的签名' => array('cc'=>'my.php?do=My.book', 'js'=>''),
		
			'我的电话' => array('cc'=>'tel.php?do=CdrMy.find', 'js'=>''),
			'我的网络' => array('cc'=>'log.php?do=NetworkMy.find', 'js'=>''),
		
			'我的工资' => array('cc'=>'finance.php?do=SalaryMy.find', 'js'=>''),
			'我的物品' => array('cc'=>'finance.php?do=AssetMy.find', 'js'=>''),
			'报废信息' => array('cc'=>'finance.php?do=ScrapMy.find', 'js'=>''),
			// '借物管理' => array('cc'=>'finance.php?do=MyAssetBorrow.find', 'js'=>''),
		    // '我的试卷' => array('cc'=>'resume.php?do=MyExam.my', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function mobile($controller, $request) {
		$this->pretreat($controller);
	
		global $CategoryTree;
	
		$data = $CategoryTree['我的信息'];
	
		$ret = array();
		foreach ($data as $k => $v) {
			if ($this->tree_my($v)) {
				$ret[$k] = $v;
			}
		}
	
		$controller->assign('menu', $ret);
	
		$controller->display($request, 'mobile');
		return true;
	}
	
	function tab($controller, $request) {
		$data = array(
			// '便笺管理' => array('cc'=>'my.php?do=MyNote.find', 'js'=>''),
			'任务信息' => array('cc'=>'mold.php?do=PlanMy.find', 'js'=>'1'),
			'我的工作' => array('cc'=>'emp.php?do=PlanMy.find', 'js'=>'1'),
				
			'用车需求' => array('cc'=>'car.php?do=RequirementMy.find', 'js'=>'1'),
			'货运需求' => array('cc'=>'express.php?do=RequirementMy.find', 'js'=>'1'),
				
			'业务需求' => array('cc'=>'emp.php?do=BusinessMy.find', 'js'=>'1'),
		
			'我的合同' => array('cc'=>'finance.php?do=ContractMy.find', 'js'=>''),
				
			'我的会议' => array('cc'=>'info.php?do=ConferenceMy.find', 'js'=>''),
			'员工信息' => array('cc'=>'emp.php?do=EmpMy.find', 'js'=>''),
			'二维条码' => array('cc'=>'info.php?do=QrMy.find', 'js'=>''),
		
			'我的试卷' => array('cc'=>'resume.php?do=MyExam.my', 'js'=>''),
		
			'产品信息' => array('cc'=>'mold.php?do=Mold.find', 'js'=>''),
			'我的问答' => array('cc'=>'info.php?do=FaqMy.find', 'js'=>''),
		
			// '我的工作' => array('cc'=>'info.php?do=JobMy.find', 'js'=>''),
			'我的消息' => array('cc'=>'log.php?do=MessageMy.find', 'js'=>''),
			// '名片簿' => array('cc'=>'customer.php?do=PotentialMy.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tec($controller, $request) {
		$data = array(
			'便笺管理' => array('cc'=>'info.php?do=NoteMy.find', 'js'=>''),
		
			'任务信息' => array('cc'=>'mold.php?do=PlanMy.find', 'js'=>''),
			'物料需求' => array('cc'=>'material.php?do=Bom.find', 'js'=>''),	
			'用车需求' => array('cc'=>'car.php?do=RequirementMy.find', 'js'=>''),
			'货运需求' => array('cc'=>'express.php?do=RequirementMy.find', 'js'=>''),
			
			'发送传真' => array('cc'=>'fax.php?do=Fax.add', 'js'=>''),				
			// '产品信息' => array('cc'=>'mold.php?do=Mold.find', 'js'=>''),
			'我的会议' => array('cc'=>'info.php?do=ConferenceMy.find', 'js'=>''),
		
			'物料库存' => array('cc'=>'material.php?do=Amount.find', 'js'=>''),
		
			'报销信息' => array('cc'=>'finance.php?do=ExpenseMy.find', 'js'=>''),			
			'付款信息' => array('cc'=>'finance.php?do=PaymentMy.find', 'js'=>''),
		
			'我的加班' => array('cc'=>'kq.php?do=OvertimeMy.find', 'js'=>''),
		
			'我的物品' => array('cc'=>'finance.php?do=AssetMy.find', 'js'=>''),
		
			'我的电话' => array('cc'=>'tel.php?do=CdrMy.find', 'js'=>''),		
			'我的网络' => array('cc'=>'wifi.php?do=LogMy.find', 'js'=>''),		
			'我的通知' => array('cc'=>'log.php?do=NoticeMy.find', 'js'=>''),
					
			'最新汇率' => array('cc'=>'finance.php?do=Finance.roe', 'js'=>''),			
		);
		
		$ret = array();
		while (list($k, $v) = each($data)) {
			if ($this->tree_my($v)) {
				// $ret[$k] = $v;
				$v['title'] = $k;
				$ret[] = $v; 
			}
		}
		
		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'my/tab');
		return true;
	}
	
	function finance($controller, $request) {		
		$data = array(
			'报销信息' => array('cc'=>'finance.php?do=ExpenseMy.find', 'js'=>'1'),			
			'付款信息' => array('cc'=>'finance.php?do=PaymentMy.find', 'js'=>'1'),
			
			'收款信息' => array('cc'=>'finance.php?do=InMy.find', 'js'=>'1'),
			'收款明细' => array('cc'=>'finance.php?do=InItemMy.find', 'js'=>'1'),
			'支出信息' => array('cc'=>'finance.php?do=OutMy.find', 'js'=>'1'),
			'支出明细' => array('cc'=>'finance.php?do=OutItemMy.find', 'js'=>'1'),
			
			'提成信息' => array('cc'=>'finance.php?do=CommissionMy.find', 'js'=>''),
			'借款信息' => array('cc'=>'finance.php?do=BorrowMy.find', 'js'=>'1'),		
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function setting($controller, $request) {		
		$data = array(			
			'密码管理' => array('cc'=>'emp.php?do=PasswordMy.layout', 'js'=>''),
			'分机设置' => array('cc'=>'tel.php?do=CdrMy.setting', 'js'=>''),
				
			'帐号信息' => array('cc'=>'info.php?do=AccountMy.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $_SESSION['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		
		$row['dept_t'] = $this->_dept($row['dept']);

		$row['sex'] = $this->sex[$row['sex']];
		$row['education'] = $this->education[$row['education']];
		$row['marriage'] = $this->marriage[$row['marriage']];
		$row['nationality'] = $this->nationality[$row['nationality']];

		$controller->assign_by_ref('emp', $row);
		$controller->assign_by_ref('result', $row);
		
		$controller->assign_by_ref('bank', $this->_bank($id));
			
		$controller->assign_by_ref('family', $this->_my_additional(Constant::tbl_emp_family, $id));
		$controller->assign_by_ref('education', $this->_my_additional(Constant::tbl_emp_education, $id));
		$controller->assign_by_ref('work', $this->_my_additional(Constant::tbl_emp_work, $id));

		$controller->display($request, 'my/view');
		return true;
	}
	
	function _bank($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = '1' AND info_id = '%s' AND status = '1'";
		$sql = sprintf($fmt, Constant::tbl_cw_bank, $id);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['code'] = $this->_format($row['code'], 4, ' ');
			
			$ret[] = $row;
		}
		
		return $ret;
	}

	/**
	 * 修改密码
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function password($controller, $request) {

		$controller->display($request, 'my/password');
		return true;
	}

	/**
	 * 修改密码
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function passwordPost($controller, $request) {
		$this->pretreat($controller);

		$id = $_SESSION['id'];
		
		$oldpassword = $request['password'];
		$password = $request['newpassword'];
		$repassword = $request['renewpassword'];
		
		$ret = array();

		$db = newdb('emp');
		$fmt = "SELECT * FROM %s WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($this->_password($row['password'], $oldpassword)) {
				$ret['status'] = 2;
				echo json_encode($ret);
				return true;
			}
		}

		$fmt = "UPDATE %s SET password = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $password, $id);
		// debug($sql);
		$db->query($sql);

		$ret['status'] = 1;
		echo json_encode($ret);
		return true;
	}

	function _password($password, $input) {
		$s = trim($input);
		if (strlen($s) == 0) {
			return true;
		}

		if ($password == $input) {
			return false;
		}

		if ($password == md5($input)) {
			return false;
		}

		return true;
	}
	
	function notice_my($controller, $request) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s ORDER BY ID DESC LIMIT 2";
		$sql = sprintf($fmt, Constant::tbl_notice);
		debug($sql);
		$result = $db->query($sql);
		$data = $db->fetch_all($result);
		
		echo json_encode($data);
	}
	
	/**
	 * 修改密码
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function book($controller, $request) {

		$controller->display($request, 'my/book');
		return true;
	}
}
?>